package com.hmy.cornpestdet.utils;

import com.hmy.cornpestdet.pojo.MyPoint;

public class PositionUtil {
    /**
     * 赤道半径（单位：米）
     */
    private static final double EQUATOR_RADIUS = 6378137;

    public static double getDistance1(MyPoint point1, MyPoint point2) {
        // 纬度
        double lat1 = Math.toRadians(point1.getLat());
        double lat2 = Math.toRadians(point2.getLat());
        // 经度
        double lon1 = Math.toRadians(point1.getLng());
        double lon2 = Math.toRadians(point2.getLng());
        // 纬度之差
        double a = lat1 - lat2;
        // 经度之差
        double b = lon1 - lon2;
        // 计算两点距离的公式
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(b / 2), 2)));
        // 弧长乘赤道半径, 返回单位: 千米
        s = s * EQUATOR_RADIUS / 1000;
        return s;
    }
}
